/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 编辑器;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; 
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author yanxi
 */
public class 编辑Panel extends javax.swing.JPanel {
    主界面 frame;
    public File file;
    Document dom; 
    /**
     * Creates new form XML文件编辑Panel
     */
    public 编辑Panel(主界面 frame) { 
        initComponents();  
        
        this.frame=frame;
        this.file=new File("新文件.xml"); 
        
        try {
            DocumentBuilder db=DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
            dom=db.newDocument(); 
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(编辑Panel.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        jTree元素.setModel(new DefaultTreeModel(createRootNode())); 
    }
    public 编辑Panel(主界面 frame,File file) { 
        initComponents();
        
        this.frame=frame;
        this.file=file; 
        
        try {
            DocumentBuilder db=DocumentBuilderFactory.newInstance().newDocumentBuilder();
            dom=db.parse(file);
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            Logger.getLogger(编辑Panel.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        jTree元素.setModel(new DefaultTreeModel(createRootNode())); 
    }
    
    public DefaultMutableTreeNode createRootNode(){ 
        for(int i=0;i<dom.getChildNodes().getLength();i++){ 
            if(dom.getChildNodes().item(i).getNodeType()==Node.ELEMENT_NODE){
                return new 树节点(dom.getChildNodes().item(i)).createTreeNode();  
            }
        } 
        return null;
    }
    
    public void setAttributeTable(Node n){
        Vector 表头=new Vector();
        表头.add("属性名称");
        表头.add("属性内容");
        
        Vector 表数据=new Vector();
        NamedNodeMap map=n.getAttributes();
        if(n.hasAttributes()){  
            for(int i=0;i<map.getLength();i++){
                Node node=map.item(i);
                Vector 记录=new Vector();
                记录.add(node.getNodeName());
                记录.add(node.getNodeValue()); 
                表数据.add(记录); 
            } 
        }
         
        jTable属性列表.setModel(new DefaultTableModel(表数据, 表头) {
            @Override
            public boolean isCellEditable(int row, int column) {
                JTextField tf = new JTextField();
                tf.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) { 
                        Attr node=(Attr)map.item(row);
                        if(column==0){ 
                            map.removeNamedItem(node.getNodeName()); 
                            node=n.getOwnerDocument().createAttribute(tf.getText());
                            String v=""+jTable属性列表.getModel().getValueAt(row, 1);
                            node.setValue(v);  
                        }
                        if(column==1){ 
                            node.setValue(tf.getText());  
                        }
                        map.setNamedItem(node);
                        System.out.println("actionPerformed:"+node.getName()+","+node.getValue());
                        jTree元素.updateUI();
                    }
                });
                tf.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
                tf.setSelectionStart(0);
                tf.setSelectionEnd(tf.getText().length());
                jTable属性列表.getColumnModel().getColumn(column).
                        setCellEditor(new DefaultCellEditor(tf));
                return true;
            }
        });
    } 
    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel属性 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable属性列表 = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree元素 = new javax.swing.JTree();

        setLayout(new java.awt.BorderLayout());

        jSplitPane1.setDividerLocation(400);
        jSplitPane1.setDividerSize(10);
        jSplitPane1.setOneTouchExpandable(true);

        jPanel属性.setLayout(new java.awt.BorderLayout());

        jTable属性列表.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jTable属性列表);

        jPanel属性.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jSplitPane1.setRightComponent(jPanel属性);

        jPanel4.setLayout(new java.awt.BorderLayout());

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        jTree元素.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTree元素.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree元素ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTree元素);

        jPanel4.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jSplitPane1.setLeftComponent(jPanel4);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jTree元素ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree元素ValueChanged
        if(jTree元素.getSelectionPath()==null)return;
        DefaultMutableTreeNode node=(DefaultMutableTreeNode)jTree元素.getSelectionPath().getLastPathComponent();
        setAttributeTable(((树节点)node.getUserObject()).n);
        this.updateUI();

        if(node.getParent()==null){
            frame.jMenuItem删除节点.setEnabled(false);
            frame.jMenuItem剪切节点.setEnabled(false);
        }
        else{
            frame.jMenuItem删除节点.setEnabled(true);
            frame.jMenuItem剪切节点.setEnabled(true);
        }
        if(frame.复制的节点!=null)
            frame.jMenu粘贴节点.setEnabled(true);
        else
            frame.jMenu粘贴节点.setEnabled(false);
    }//GEN-LAST:event_jTree元素ValueChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel属性;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    public javax.swing.JTable jTable属性列表;
    public javax.swing.JTree jTree元素;
    // End of variables declaration//GEN-END:variables
}
